__author__ = 'mtxrym'

import matplotlib.pyplot as plt
def make_list(h):
    h_n = []
    sum = 0
    for p in h:
        sum += int(p)
        h_n.append(sum)
    return h_n

def get_top_id(ps):
    mmax = []
    for h in ps:
        if len(h) > 1:
            mmax.append(max(make_list(h[1:])))
    mmax.sort(reverse = True)
    if len(mmax) > 5:
        return mmax[0:5]
    else:
        return mmax


def plot_from_file(FILE_NAME):
    plotsrc = {}
    with open(FILE_NAME) as csvfile:
        for line in csvfile:
            value = line.split(",")
            a = value[0]
            tid = value[2]
            b = [tid] + value[3:-1]
            ttime = value[1]

            if a not in plotsrc.keys():
                plotsrc[a] = []
            else:
                plotsrc[a].append(b)
        for item in plotsrc:
            if len(plotsrc[item]) > 2:
                llist = get_top_id(plotsrc[item])
                for h in plotsrc[item]:
                    if len(h) > 1 and max(make_list(h[1:])) in llist:
                        plt.plot(make_list(h[1:]), label=h[0])
                    else:
                        plt.plot(make_list(h[1:]))
                plt.legend(loc = "lower right")
                plt.title(item)
                ax = plt.gca()
                ax.grid(True)
                plt.show()


if __name__ == "__main__":

    plot_from_file("/Users/mtxrym/egg.csv")